package com.cwx.mychat.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.protocol.JacksonJsonSupport;
import com.cwx.mychat.config.properties.SocketIoProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作者： cwx
 * 时间： 2025/2/13 14:13
 * 版本： 1.0
 * 注释：
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(SocketIoProperties.class)
public class SocketIoConfig {


    @Resource
    private SocketIoProperties socketIoProperties;

    /**
     * netty-socketio服务器
     * @return
     */
    @Bean
    public SocketIOServer socketIoService(){
        com.corundumstudio.socketio.Configuration configuration = new com.corundumstudio.socketio.Configuration();
        configuration.setHostname(socketIoProperties.getHost());
        configuration.setPort(socketIoProperties.getPort());
        configuration.setBossThreads(socketIoProperties.getBossThreads());
        configuration.setWorkerThreads(socketIoProperties.getWorkerThreads());
        configuration.setAllowCustomRequests(socketIoProperties.getAllowCustomRequests());
        configuration.setUpgradeTimeout(socketIoProperties.getUpgradeTimeout());
        configuration.setPingTimeout(socketIoProperties.getPingTimeout());
        configuration.setPingInterval(socketIoProperties.getPingInterval());
        configuration.setOrigin(socketIoProperties.getOrigin());
        configuration.setAllowHeaders("Authorization");
        SocketIOServer socketIOServer = new SocketIOServer(configuration);
        log.info("--------socketio server 配置加载成功--------");
        return socketIOServer;
    }

    /**
     * 扫描注解,例如@OnConnect、@OnDisconnect、@OnError、@OnEvent
     * @return
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer){
        return new SpringAnnotationScanner(socketServer);
    }
}
